package com.tank.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class SaveImage {
	public void copyImage(File oldFile, File newFile) {
		InputStream inputStream=null;
		OutputStream outputStream =null;
		try {
			inputStream = new FileInputStream(oldFile);
			outputStream = new FileOutputStream(newFile);
			byte[] b = new byte[102400];
			int read =-1;
			while((read=inputStream.read(b, 0, b.length))!=-1){
				outputStream.write(b, 0, read);
			}
			outputStream.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(inputStream!=null){
				try {
					inputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(outputStream!= null){
				try {
					outputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		oldFile.delete();
	}
	
	public String makeNewFileName(String oldFileName){
		String ext = oldFileName.substring(oldFileName.lastIndexOf("."));

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		String time = simpleDateFormat.format(new Date());
		
		Random random = new Random();
		int r = random.nextInt(10000);
		String newFileName = time + r + ext;
		
		return newFileName;
	}
}
